package com.example.demo6.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo6.pojo.Student;
import com.example.demo6.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping("findPage")
    public String findAll(Model model, Integer pageIndex, Student student){
        if (pageIndex==null)pageIndex=1;
        Page<Student> page=studentService.findAll(pageIndex,student);
        model.addAttribute("page",page);
        model.addAttribute("studentQ",student);
        return "list";
    }
    @GetMapping("toAdd")
    public String toAdd(){
        return "add";
    }
    @PostMapping("add")
    public String add(Student student){
        studentService.add(student);
        return "redirect:findPage";
    }
    @GetMapping("findById")
    public  String findById(Model model,Integer id){
        Student student=studentService.findById(id);
        model.addAttribute("s",student);
        return "edit";
    }
    @PostMapping("edit")
    public String edit(Student student){
        studentService.edit(student);
        return "redirect:findPage";
    }
    @GetMapping("del")
    public String del(Integer id){
        studentService.del(id);
        return "redirect:findPage";
    }

}
